//
//  STBaseCommodityController.swift
//  iPhone
//
//  Created by Toj on 8/10/22.
//

import UIKit

let goPayViewHeight: CGFloat = 100
let headerViewHeight: CGFloat = 104
let segmentHeight: CGFloat = 40
let accountInfoHeight: CGFloat = 64

protocol STCommodityControllerDelegate: NSObjectProtocol {
    func commodityController(_ vc: STBaseCommodityController, scrollView: UIScrollView, headerAlpha: CGFloat)
    
    func commodityController(_ vc: STBaseCommodityController, scrollView: UIScrollView, isShow: Bool)
}

class STBaseCommodityController: STBaseViewController {
    
    weak var commodityDelegate: STCommodityControllerDelegate?
    
    weak var headerView: CommodityHeaderView? {
        didSet {
            headerView?.removeFromSuperview()
            guard let headView = headerView else { return }
            collectionView.addSubview(headView)
            headView.frame = CGRect(x: 0, y: -headerViewHeight, width: CGFloat.sr_width, height: headerViewHeight)
        }
    }
    
    var type: CommoditySegmentType = .mobile
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.bounces = false
        view.scrollsToTop = true
        view.alwaysBounceVertical = true
        view.showsVerticalScrollIndicator = false
        view.backgroundColor = .white
        view.contentInset = UIEdgeInsets(top: headerViewHeight, left: 0, bottom: 0, right: 0)
        
        return view
    }()
    
    var bottomOffset: CGFloat = 0
    
    var observerOffset: CGPoint? {
        didSet {
            let offsetY = observerOffset?.y ?? 0
            var bottom = CGFloat.zero
            if offsetY > 0 {
                bottom = bottomOffset
            }
            collectionView.contentInset = UIEdgeInsets(top: headerViewHeight, left: 0, bottom: bottom, right: 0)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
        
        registerCollectionViews()
    }
    
    func registerCollectionViews() {
        // header
        collectionView.register(CommodityTitleView.cell_class, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: CommodityTitleView.cell_identifier)
        // footer
        collectionView.register(UICollectionReusableView.cell_class, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: UICollectionReusableView.cell_identifier)
    }
    
    public func scrollToTop() {
        collectionView.setContentOffset(CGPoint(x: 0, y: -headerViewHeight), animated: false)
    }
    
    private func contentOffsetHandle(_ offset: CGPoint) {
        let offsetY = offset.y
        let headerViewHidden = headerView?.isHidden ?? true
        if -offsetY > segmentHeight && !headerViewHidden { return }
        // print("-->toj: offsetY:\(offsetY)")
        if offsetY > 0 {
            if let obsOffset = observerOffset {
                let isShow = offsetY > obsOffset.y
                commodityDelegate?.commodityController(self, scrollView: collectionView, isShow: isShow)
            }
            
            if headerViewHidden {
                return
            }
        }
        headerView?.isHidden = offsetY > 0
        let alpha = (segmentHeight + offsetY) / segmentHeight
        commodityDelegate?.commodityController(self, scrollView: collectionView, headerAlpha: alpha)
    }
}

extension STBaseCommodityController: UICollectionViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        contentOffsetHandle(offset)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        contentOffsetHandle(offset)
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        contentOffsetHandle(offset)
    }
}
